<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属区域" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入所属区域"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择">
          <el-option
            v-for="item in stateOptions"
            :key="item.value"
            :label="item.label" @change="handleQuery"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

  
    <el-tabs v-model="tabActiveName"  @tab-click="handleTabClick">
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <el-tab-pane label="待审核" name="1"></el-tab-pane>
      <el-tab-pane label="已通过" name="2"></el-tab-pane>
      <el-tab-pane label="未通过" name="3"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="公司名称" align="left" prop="name" />
      <el-table-column label="法人名称" align="left" prop="frName" />
      <el-table-column label="法人电话" align="center" prop="frPhone" />
      <el-table-column label="信用代码" align="center" prop="xyCode" />
      <el-table-column label="法人身份证号" align="center" prop="idcard" >
        <template slot-scope="scope">
          {{scope.row.idcard}}
        </template>  
      </el-table-column>        
      <el-table-column label="营业执照照片" align="center" prop="cardZheng" >
        <template slot-scope="scope">
          <el-image 
            style="width: 60px; height: auto"
            :src="scope.row.cardZheng" 
            :preview-src-list="[scope.row.cardZheng]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="开户许可证明" align="center" prop="cardFan" >
        <template slot-scope="scope">
          <el-image 
            style="width: 60px; height: auto"
            :src="scope.row.cardFan" 
            :preview-src-list="[scope.row.cardFan]">
          </el-image>
        </template>
      </el-table-column>
      <!-- <el-table-column label="营业执照照片" align="center" prop="yingyePic" >
        <template slot-scope="scope">
          <el-image 
            style="width: 60px; height: auto"
            :src="scope.row.yingyePic" 
            :preview-src-list="[scope.row.yingyePic]">
          </el-image>
        </template>
      </el-table-column> -->
      <el-table-column label="公司地址" align="left" prop="address" width="200" />
      <!-- <el-table-column label="经度" align="center" prop="x" /> -->
      <!-- <el-table-column label="纬度" align="center" prop="y" /> -->
      <!-- <el-table-column label="所属区域" align="center" prop="area" /> -->
      <el-table-column label="状态" align="center" prop="state" >
        <template slot-scope="scope">
          {{stateOptions.find(a=>a.value == scope.row.state).label}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coordinate" v-if="scope.row.state === '1'"
            @click="handleApprove(scope.row)"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search" 
            @click="handleShow(scope.row)"
          >查看</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets" 
            @click="handleOrderList(scope.row)"
          >接单记录</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" style=""  :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :disabled="disabled">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="公司名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用代码" prop="xyCode">
              <el-input v-model="form.xyCode" placeholder="请输入信用代码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="法人代表" prop="frName">
              <el-input v-model="form.frName" placeholder="请输入法人代表名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="frPhone">
              <el-input v-model="form.frPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="法人身份证号" prop="idcard">
              <el-input v-model="form.idcard" placeholder="请输入法人身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="营业执照照片" prop="cardZheng">
              <imageUpload v-model="form.cardZheng"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户许可证明" prop="cardFan">
              <imageUpload v-model="form.cardFan"/>
            </el-form-item>
          </el-col>
        </el-row>
       
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属区域" prop="area">
              <el-cascader 
                  :value="form.areaList"
                  :options="areaOptions"
                  @change="areaChange"
                  style="width: 100%;"
                  :show-all-levels="true" :props="props"></el-cascader>
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="公司地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入公司地址" />
          </el-form-item>
          </el-col>
        </el-row>
        
      
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="submitPass" v-if="disabled && approved">通 过</el-button>
        <el-button type="warning" @click="submitNotPass" v-if="disabled && approved">驳 回</el-button>
        <el-button type="primary" @click="submitForm" v-if="!disabled">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 订单管理对话框 -->
    <el-dialog title="订单管理" :visible.sync="orderShow" width="50%" append-to-body>
      <el-table v-loading="orderloading" :data="orderList" show-summary>
          <el-table-column label="订单编号" align="center" prop="num" width="150" >
          </el-table-column>
          <el-table-column label="订单信息" align="center" prop="detail" />
          <el-table-column label="预约时间" align="center" prop="orderTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="联系人" align="center" prop="username" />
          <!-- <el-table-column label="联系电话" align="center" prop="telephone" /> -->
          <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"  />
          <el-table-column label="备注" align="center" prop="remark" />
          <!-- <el-table-column label="应付" align="center" prop="payable" /> -->
          <el-table-column label="实付" align="center" prop="pay" />
        </el-table>
        
        <pagination
          v-show="orderTotal>0"
          :total="orderTotal"
          :page.sync="queryOrderParams.pageNum"
          :limit.sync="queryOrderParams.pageSize"
          @pagination="getOrderList"
        />
        <div slot="footer" class="dialog-footer" style="text-align:center;">
          <el-button @click="orderShow = false">返 回</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompany, getCompany, delCompany, addCompany, updateCompany, exportCompany } from "@/api/business/company";
import ImageUpload from '@/components/ImageUpload';
import { treeArea } from "@/api/system/area";
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/business/order";


export default {
  name: "Company",
  components: {
    ImageUpload
  },
  data() {
    return {
      tabActiveName: 'all',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      orderloading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      orderShow: false,
      // 总条数
      total: 0,
      orderTotal: 0,
      // 公司管理表格数据
      companyList: [],
      orderList: null,
      areaOptions:[],
      // 状态数据字典
      statusOptions: [],
      props:{
        value: 'id',
        label: 'name',
        expandTrigger: 'hover'
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      disabled: false,
      approved: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        area: null,
        state: null
      },
      company: null,
      // 查询参数
      queryOrderParams: {
        pageNum: 1,
        pageSize: 10,
        num: null,
        detail: null,
        orderTime: null,
        userId: null,
        username: null,
       
      },
      stateOptions: [{
          value: '0',
          label: '未审核'
        }, {
          value: '1',
          label: '审核中'
        }, {
          value: '2',
          label: '审核通过'
        }, {
          value: '3',
          label: '驳回'
        }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        frName: [
          { required: true, message: "法人名称不能为空", trigger: "blur" }
        ],
        xyCode: [
          { required: true, message: "信用代码不能为空", trigger: "blur" }
        ],
        idcard: [
          { required: true, message: "法人身份证号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAreaTree();
    this.getDicts("tb_order_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询公司管理列表 */
    getList() {
      this.loading = true;
      // this.queryParams.state = 1;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAreaTree() {
      treeArea().then(response => {
        this.areaOptions = response.data;
      });
    },
    /** 查询订单管理列表 */
    getOrderList() {
      this.orderloading = true;
      this.queryOrderParams.params = {};
      this.queryOrderParams.companyId = this.company.id;
      if (null != this.daterangeOrderTime && '' != this.daterangeOrderTime) {
        this.queryOrderParams.params["beginOrderTime"] = this.daterangeOrderTime[0];
        this.queryOrderParams.params["endOrderTime"] = this.daterangeOrderTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryOrderParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryOrderParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listOrder(this.queryOrderParams).then(response => {
        this.orderList = response.rows;
        this.orderTotal = response.total;
        this.orderloading = false;
      });
    },
    handleTabClick(tab){
      if (tab.name != 'all') {
        this.queryParams.state = tab.name;
      }else{
        this.queryParams.state = '';
      }
      this.getList();

    },
    handleOrderList(row) {
      this.orderShow = true;
      this.company = row;
      this.getOrderList();
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        frName: null,
        frPhone: null,
        xyCode: null,
        idcard: null,
        cardZheng: null,
        cardFan: null,
        yingyePic: null,
        address: null,
        x: null,
        y: null,
        area: null,
        createId: null,
        createTime: null,
        state: 2
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCompany(id).then(response => {
        this.form = response.data;
        let areaCode = response.data.area; 
        this.form.areaList = [areaCode.substr(0,2)+"0000", areaCode.substr(0,4)+"00", areaCode].map(a=>parseInt(a));
        this.disabled = false;
        this.approved = false;
        this.open = true;
        this.title = "修改公司信息";
      });
    },
    handleApprove(row) {
      const id = row.id || this.ids
      getCompany(id).then(response => {
        this.form = response.data;
        let areaCode = response.data.area; 
        this.form.areaList = [areaCode.substr(0,2)+"0000", areaCode.substr(0,4)+"00", areaCode].map(a=>parseInt(a));
        this.disabled = true;
        this.approved = true;
        this.open = true;
        this.title = "审核公司信息";

      });
    },
    areaChange(node){
      this.form.area = node[node.length -1 ];
    },
    handleShow(row) {
      const id = row.id || this.ids
      getCompany(id).then(response => {
        this.form = response.data;
        this.disabled = true;
        this.approved = false;
        this.open = true;
        this.title = "查看公司信息";

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {         
          if (this.form.id != null) {
            updateCompany(this.form).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitPass() {
      this.$refs["form"].validate(valid => {
        if (valid) {   
          this.form.state = 2;      
          if (this.form.id != null) {
            updateCompany({
              id: this.form.id,
              state: this.form.state
            }).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitNotPass() {
      this.$refs["form"].validate(valid => {
        if (valid) {   
          this.form.state = 3;      
          if (this.form.id != null) {
            updateCompany({
              id: this.form.id,
              state: this.form.state
            }).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除公司管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompany(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有公司管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCompany(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
